<?php
if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
    exit('Access Denied');
}

// 获取表模型
$table_configs = C::t('#erling_kami_member#config_er_kami_member');

// 处理表单提交
if(submitcheck('configsubmit')) {
    $configs = $_POST['config'];
    foreach($configs as $key => $value) {
        if($key == 'nav_name') {
            $value = str_replace('<script>', '', $value);
        }
        $result = $table_configs->update_by_key($key, array('value' => $value));
        if ($result['success'] === false) {
            $errors[] = "配置项 {$key} 更新失败：{$result['message']}";
        }
    }
    if(empty($errors)) {
        cpmsg('配置更新成功', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_kami_member&pmod=admin_config', 'succeed');
    } else {
        cpmsg(implode('<br>', $errors), 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_kami_member&pmod=admin_config', 'error');
    }
}

// 获取当前配置
$configs = $table_configs->fetch_all();

// 获取用户组
$all_groups = C::t('common_usergroup')->range();
$system_groups = [];// 系统用户组
$member_groups = [];// 会员用户组
$special_groups = [];// 自定义用户组  
$admin_groups = [];// 自定义管理组    
foreach($all_groups as $group) {
    if($group['type'] == 'special') {
        if($group['radminid'] > 0) {
            $admin_groups[] = $group;
        } else {
            $special_groups[] = $group;
        }
    }
    if($group['type'] == 'system') {
        $system_groups[] = $group;
    }
    if($group['type'] == 'member') {
        $member_groups[] = $group;
    }
}


include template('erling_kami_member:admin_config');
